/*
 * Copyright 2008-2011 Wyld Collective / Obx Labs
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

/**
 * FeedEventCallback
 *
 * This example shows how to use callbacks to catch and respond to
 * events in the Feeder when data is fetched from a syndicated
 * feed (RSS or ATOM). The sketch fetches information
 * from a feed, prints out its entries to the console, and continues
 * pulling entries from the feed at interval if any are available,
 * and calls preset methods in the process.
 */

//romefeeder
import com.wyldco.romefeeder.*;
import com.sun.syndication.feed.synd.*;

Feeder feeder;          // the feeder
SyndEntry entry;        // a feed entry
boolean loaded = false; // flag to true when the feed is loaded

void setup() {
  //create the feeder
  feeder = new Feeder(this);

  //turn on output to the console (useful for debugging)
  feeder.verbose = true;

  //set sort by published date
  //default is unsorted (i.e. as ordered in the feed)
  feeder.sortByPublishedDate();

  //if you are exporting your sketch to an applet
  //set the proxy to bypass applet domain limitations
  //make sure you copy the proxy.php provided in the
  //data folder of example SimpleUrlFeed to your applet directory
  //feeder.setProxy("http://[urlToApplet]/FeedEventCallback/applet/proxy.php");

  //set the feed load callback, which is triggered after a feed is loaded
  //look at the romefeeder documentation to see all available callbacks
  feeder.setLoadFeedCallback(
      new FeedCallback() { public void event(SyndFeed feed) { postLoadFeed(feed); } }
  );

  //there is also some callback that relate to entries and those are
  //a bit different, they use an EntryCallback object, and their event
  //function received both the entry object, and the feed its feed
  feeder.setLoadEntryCallback(
      new EntryCallback() { public void event(SyndEntry entry, SyndFeed feed) { postLoadEntry(entry, feed); } }
  );
      
  //set the update interval to check for new posts in the loaded feed(s)
  feeder.setUpdateInterval(5*60*1000); // milliseconds
  
  //start updating
  feeder.startUpdate();
    
  //load the feed  
  feeder.load("http://www.thestranger.com/seattle/Rss.xml?category=258");
}

void draw() {
  //if the feed is not leaded, nothing to do
  if (!loaded) return;
  
  //display the next entries
  while (feeder.hasNext()) {
    //get the next entry
    entry = feeder.next();
    println("\n" + entry.getTitle());
    println(" + " + entry.getPublishedDate());
    println(" + " + entry.getLink());
  }
}

//called after a feed is loaded, as defined in setup()
void postLoadFeed(SyndFeed feed) {
  loaded = true;
}

//called after an entry is loaded, as defined in setup()
void postLoadEntry(SyndEntry entry, SyndFeed feed) {
  //do something useful here
  println("Loaded entry: " + entry.getTitle());
}